/** Ares Eventos
 * By AsuS
 * Para AreS Community GameS
 * www.ares-games.net 
 * Hide&Seek Event
 */

package com.l2jserver.gameserver.model.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javolution.util.FastMap;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.l2jserver.Config;
import com.l2jserver.gameserver.Announcements;

/**
 * @author BiggBoss
 */
public class HideAndSeekDataParser {

	protected static Map<Integer, String[]> _zoneData = new FastMap<Integer, String[]>();
	private String _name = null;
	private int _id;
	private int _x, _y, _z;
	private int _rewardId, _rewardAmount;
	private int _npcToSpawnId;
	private String[] _clues = new String[Config.HAS_CLUES];
	
	public static short _maxZones;
	protected boolean _stop = false;
	
	protected static enum Action {
		READ, 
		SAVE
	}
	
	protected HideAndSeekDataParser() {
		
	}
	
	protected HideAndSeekDataParser(int id) {
		this._id = id;
		if(!_zoneData.containsKey(id)) {
			System.out.println("ID "+id+" is not contained!");
			for(int i : _zoneData.keySet()) {
				System.out.println(i+"\n");
			}
		}
		_name = _zoneData.get(id)[0];
		_x = Integer.parseInt(_zoneData.get(id)[1]);
		_y = Integer.parseInt(_zoneData.get(id)[2]);
		_z = Integer.parseInt(_zoneData.get(id)[3]);
		_npcToSpawnId = Integer.parseInt(_zoneData.get(id)[4]);
		_rewardId = Integer.parseInt(_zoneData.get(id)[5]);
		_rewardAmount = Integer.parseInt(_zoneData.get(id)[6]);
		for(int s = 0; s < _clues.length; s++) {
			_clues[s] = _zoneData.get(id)[7+s];
		}
	}
	
	public static void getData() {
		Integer id = null;
		byte size = (byte) (7 + Config.HAS_CLUES);
		
		try {
			File eventXML = new File("data\\hideandseek\\HideAndSeek.xml");
			
			DocumentBuilderFactory docBuilderF = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderF.newDocumentBuilder();
			Document doc = docBuilder.parse(eventXML);
			
			
			NodeList nodeList = doc.getElementsByTagName("id");
			_maxZones = (short) nodeList.getLength();
				
			for(int i=0; i < _maxZones; i++) {
					
				Node n = nodeList.item(i);
				NodeList subNodes = null;
				
				if(n.getNodeName() == "id") {
					id = Integer.valueOf(n.getAttributes().getNamedItem("value").getNodeValue());
					subNodes = n.getChildNodes();
				}
				
				String[] data = new String[size];
				for(byte c = 0; c < subNodes.getLength(); c++) {
					
					Node child = subNodes.item(c);
					
					if(child.getNodeName() == "zone") 
						data[0] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "x")
						data[1] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "y")
						data[2] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "z")
						data[3] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "npcId")
						data[4] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "rewardId" && !Config.HAS_SINGLEREWARD)
						data[5] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "rewardAmount" && !Config.HAS_SINGLEREWARD)
						data[6] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "clue1") 
						data[7] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "clue2") 
						data[8] = child.getAttributes().getNamedItem("value").getNodeValue();
					else if(child.getNodeName() == "clue3") 
						data[9] = child.getAttributes().getNamedItem("value").getNodeValue();
					
					if(Config.HAS_CLUES > 3) {
						for(byte b = 4; b < Config.HAS_CLUES; b++) {
							if(child.getNodeName() == "clue"+b)
								data[6+b] = child.getAttributes().getNamedItem("value").getNodeValue();
						}
					}
				}
				_zoneData.put(id, data);
			}
			
		System.out.println("--------------------------------------------------------------------");
		System.out.println("Sucessfuly loaded " +_maxZones + " zones for the Hide And Seek AreS Event");
		System.out.println("--------------------------------------------------------------------");
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void LastZone(Action action) {
		
		try {
			File logger = new File("data\\hideandseek\\savedPlace.properties");
			FileOutputStream fos = new FileOutputStream(logger);
			FileInputStream fis = new FileInputStream(logger);
			Properties p = new Properties();
			p.load(fis);
			
			if(action.equals(Action.READ)) {
				this._id = Short.valueOf(p.getProperty("LastPlace"));
			}
			else {
				int futureId = this._id++;
				if(futureId > _maxZones)
					futureId = 0;
				
				p.setProperty("LastPlace", String.valueOf(futureId));
				p.store(fos, "");
			}
			
			fos.close();
			fis.close();
		}
		catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	protected void startClueTask() throws InterruptedException {
		long time = Config.HAS_GAMETIME * 60000;
		double interval = (double) (time / Config.HAS_CLUES);
		long starterTime = System.currentTimeMillis();
		long finishTime = System.currentTimeMillis() + time; 
		byte i = 1;
		
		Announcements.getInstance().announceToAll("H&S 1th clue: " + this._clues[0]);
		
		while(finishTime - System.currentTimeMillis() > 0) {
			long delay = System.currentTimeMillis() - starterTime;
			
			if(_stop)
				return;
			
			if(delay - interval == 0 && i < 3) {
				Announcements.getInstance().announceToAll("H&S "+(i+1)+"th clue: " + this._clues[i]);
				interval *= 2;
				i++;
				Thread.sleep(1000);
			}
			else if(i >= Config.HAS_CLUES)
				return;
		}
	}
	
	protected void stopClueTask() {
		_stop = true;
	}
	public String getZoneName() {
		return _name;
	}

	public int getPlaceId() {
		return this._id;
	}
	
	public int getX() {
		return this._x;
	}
	
	public int getY() {
		return this._y;
	}
	
	public int getZ() {
		return this._z;
	}
	
	public int getReward() {
		return this._rewardId;
	}
	
	public int getRewardAmount() {
		return this._rewardAmount;
	}

	public int getNpcId() {
	return this._npcToSpawnId;
	}
}